package com.uenpay.im.ui.old.activity.movement;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewConfiguration;
import android.widget.TextView;

import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.weight.PagerSlidingTabStrip;

import java.lang.reflect.Field;

/**
 * @author SL
 * @created 2016-1-22 早上9:07:48
 * @copyRight http://www.qiyunxin.com
 * @function TODO
 */
public class MyMovementActivity extends FragmentActivity {

	private MyJoinedMovementFragment myJoinedMovementFragment;
	private MyPublishedMovementFragment myPublishedMovementFragment;
	private ViewPager pager = null;
	private PagerSlidingTabStrip tabs;
	private DisplayMetrics dm;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		IMApplication.getInstance().addActivity(this);
		setContentView(R.layout.activity_my_movement_layout);
		initView();
		initLisenter();
		setOverflowShowingAlways();

	}

	private void initView() {
		((TextView) findViewById(R.id.title_textview))
				.setText(R.string.my_movement);
		dm = getResources().getDisplayMetrics();
		pager = (ViewPager) findViewById(R.id.pager);
		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
		tabs.setViewPager(pager);
		setTabsValue();

	}

	private void initLisenter() {
		findViewById(R.id.back_layout).setOnClickListener(
				new OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
	}

	/**
	 */
	private void setTabsValue() {
		tabs.setShouldExpand(true);
		tabs.setDividerColor(Color.TRANSPARENT);
		tabs.setUnderlineHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 1, dm));
		tabs.setIndicatorHeight((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 4, dm));
		tabs.setTextSize((int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_SP, 16, dm));
		tabs.setIndicatorColor(Color.parseColor("#2280a9"));
		tabs.setSelectedTextColor(Color.parseColor("#2280a9"));
		tabs.setTabBackground(0);
	}

	public class MyPagerAdapter extends FragmentPagerAdapter {

		public MyPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		private final String[] titles = {
				getResources().getString(R.string.movement_my_joined),
				getResources().getString(R.string.movement_my_published) };

		@Override
		public CharSequence getPageTitle(int position) {
			return titles[position];
		}

		@Override
		public int getCount() {
			return titles.length;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				if (myJoinedMovementFragment == null) {
					myJoinedMovementFragment = new MyJoinedMovementFragment();
				}
				return myJoinedMovementFragment;
			case 1:
				if (myPublishedMovementFragment == null) {
					myPublishedMovementFragment = new MyPublishedMovementFragment();
				}
				return myPublishedMovementFragment;
			default:
				return null;
			}
		}

	}

	private void setOverflowShowingAlways() {
		try {
			ViewConfiguration config = ViewConfiguration
					.get(MyMovementActivity.this);
			Field menuKeyField = ViewConfiguration.class
					.getDeclaredField("sHasPermanentMenuKey");
			menuKeyField.setAccessible(true);
			menuKeyField.setBoolean(config, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		IMApplication.getInstance().removeActivity(this);
	}

}
